#!/bin/sh -efu

# 5.10.194-elbrus-def-alt1.12.2 -> def
get_label() { echo "${1# *}" | sed -r 's,.*elbrus-([0-9a-z]+)-.*$,\1,'; }

KVERSIONS="$(rpm -qa 'kernel-image*' \
            --qf '%{installtime} %{version}-%{name}-%{release}\n' \
        | sort -n \
        | cut -f 2 -d ' ' \
        | sed 's/kernel-image-//')"
[ -n "$KVERSIONS" ] || {
	echo "40-e2k-boot-conf: kernel image missing, exiting" >&2
	exit 0
}

cd /boot

echo "** KVERSIONS=[$KVERSIONS]" >&2
if [ "`echo $KVERSIONS | wc -w`" = 1 ]; then # 2+ labels
	echo -e "default=`get_label $KVERSIONS`\n" >> boot.conf
fi

for v in $KVERSIONS; do
	l="`get_label "$v"`"
	cat >> boot.conf <<EOF
label=$l
	partition=0
	image=/image-$v
	initrd=/initrd-$v.img
	cmdline=console=tty0 consoleblank=0 hardreset root=LABEL=ROOT

EOF
done

echo "** start of boot.conf" >&2
cat boot.conf >&2
echo "** end of boot.conf" >&2
